Notebook available in PatrikValkovic/ParticleSwarmOptimization Github repository.
Can access online at https://patrikvalkovic.github.io/ParticleSwarmOptimization/complete.html.
%config IPCompleter.greedy=True
%load_ext autoreload
%autoreload 2
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
from IPython import display
from IPython.utils import io
SHOW_GIFS = False
REPEATS=40
POP_SIZE=100
GENERATIONS=200
import os
import functools
import numpy as np
import matplotlib.pylab as plt
import cocoex
from PIL import Image
import functions as fn
import execution as exe
import plotting as plot
import inspect
import strategies
import utils
if not os.path.exists("gifs"):
os.mkdir("gifs")
strategy = [
strategies.random_walk,
functools.partial(strategies.numeric_gradient_descent, gradient_step=1e-6, computation_step=0.01),
functools.partial(strategies.RandomVelocity.execute, w=1/(2*np.log(2))),
strategies.differential_evolution,
strategies.FollowBest.execute,
strategies.RingTopology.execute,
strategies.RandomTopology.execute,
strategies.NearestTopology.execute,
strategies.Standard2006.execute,
strategies.Standard2011.execute,
]
initialization = [
np.random.uniform,
np.random.uniform,
strategies.RandomVelocity.init,
np.random.uniform,
strategies.FollowBest.init,
strategies.RingTopology.init,
strategies.RandomTopology.init,
strategies.NearestTopology.init,
strategies.Standard2006.init,
strategies.Standard2011.init,
]
names = [
"Random walks",
"Gradient descent",
"Random velocities",
"Differential evolution",
"Follow best",
"Ring topology",
"Random topology",
"Nearest topology",
"Standard 2006",
"Standard 2011",
"FST-POS",
]
colors = [ #http://flinklabs.com/labs/colors/
"#1f77b4",
"#ff7f0e",
"#2ca02c",
"#d62728",
"#9467bd",
"#8c564b",
"#e377c2",
"#7f7f7f",
"#bcbd22",
"#17becf",
"#c5b0d5",
"#c49c94",
"#f7b6d2",
"#c7c7c7",
"#dbdb8d",
"#9edae5"
]
gif_counter = 0
with fn.get_suite_wrapper() as suite:
for function_id in suite.ids(): # type: cocoex.Problem
function = suite.get_problem(function_id)
print(f"\n\n\n\n\nRunning for function f{function.id_function:02} {function.dimension}D", flush=True)
if function.dimension == 2:
plot.plot_function_3d(function)
values = []
for algorithm, init, name in zip(strategy, initialization, names):
suite.reset(); function = suite.get_problem(function_id)
populations, evaluations = exe.execute_multiple(
function,
algorithm,
initialization=init,
generations=GENERATIONS,
population_size=POP_SIZE,
repeats = REPEATS,
show_progress=True
)
values.append(evaluations)
if SHOW_GIFS and function.dimension == 2:
gif = plot.animate_movement(function, populations[0], show_progress=True)
with open(os.path.join("gifs", f"{gif_counter}.gif"), "wb") as f:
f.write(gif);
print(f"Algorithm {name}", flush=True)
print(('Found' if function.final_target_hit else 'Not found') + " best solution", flush=True)
print(f"Best evaluation: {function.best_observed_fvalue1}", flush=True)
if SHOW_GIFS and function.dimension == 2:
display.Markdown(f"")
gif_counter = gif_counter+1
suite.reset(); function = suite.get_problem(function_id)
with io.capture_output() as captured:
populations, evaluations = exe.fstpso_multiple(function, REPEATS, POP_SIZE, GENERATIONS, True)
values.append(evaluations)
if SHOW_GIFS and function.dimension == 2:
gif = plot.animate_movement(function, populations[0], show_progress=True)
with open(os.path.join("gifs", f"{gif_counter}.gif"), "wb") as f:
f.write(gif);
print(f"Algorithm FST-PSO", flush=True)
print(('Found' if function.final_target_hit else 'Not found') + " best solution", flush=True)
print(f"Best evaluation: {function.best_observed_fvalue1}", flush=True)
if SHOW_GIFS and function.dimension == 2:
display.Markdown(f"")
gif_counter = gif_counter+1
values = np.stack(values)
values = values - values.min()
plt.figure(figsize=(18,12))
plt.title(function.name)
for val, name, color in zip(values, names, colors):
plot.plot_aggregated(val, plot.popfn_median(), plot.aggfn_median(), label=name, c=color)
plot.plot_aggregated(val, plot.popfn_min(), plot.aggfn_median(), linestyle='--', c=color)
plt.yscale('log')
plt.legend()
plt.show()